<?php
require_once(dirname(dirname(__FILE__)) . '/app.php');

need_partner();
$partner_id = abs(intval($_SESSION['partner_id']));
$login_partner = Table::Fetch('partner', $partner_id);

$condition = array(
	'state' => 'pay',
	'team_id > 0',
	'partner_id' => $partner_id,
);

/* filter */
$uemail = strval($_GET['uemail']);
if ($uemail) {
	$field = strpos($uemail, '@') ? 'email' : 'username';
	$field = is_numeric($uemail) ? 'id' : $field;
	$uuser = Table::Fetch('user', $uemail, $field);
	if($uuser) $condition['user_id'] = $uuser['id'];
	else $uemail = null;
}
$id = abs(intval($_GET['id'])); if ($id) $condition['id'] = $id;
$team_id = abs(intval($_GET['team_id']));
if ($team_id) {
	$condition['team_id'] = $team_id;
} else { $team_id = null; }
$is_send=$_GET['is_send'];
if ($is_send=='Y') {
	$condition[] = "express_id != '0'";
}elseif($is_send=='N'){
	$condition['express_id'] = '0';
}else{$is_send = null;}

$cbday = strval($_GET['current_datetime']);//获取时间，今天：today，昨天：yesterday，最近七天：last_seven_days，最近一个月：last_month，全部：all
$now = date("Y-m-d", time());
$now = strtotime($now);
$timenow = time();
if (!$cbday || $cbday == 'today') { //默认显示当日所产生的订单
	$condition[] = "pay_time >= $now";
}
elseif ($cbday == 'yesterday') { 
	$x = $now-86400;
	$condition[] = "pay_time >= $x";
	$condition[] = "pay_time <= $now";
}
elseif ($cbday == 'last_seven_days') { 
	$x = $now-(86400*7);
	$condition[] = "pay_time >= $x";
	$condition[] = "pay_time <= $now";
}
elseif ($cbday == 'last_month') { 
	$x = $now-(86400*30);
	$condition[] = "pay_time >= $x";
	$condition[] = "pay_time <= $now";
}
elseif ($cbday == 'all') { 
	$condition[] = "pay_time > 0";
}
/* end filter */

$count = Table::Count('order', $condition);
list($pagesize, $offset, $pagestring) = pagestring($count, 20);

$orders = DB::LimitQuery('order', array(
	'condition' => $condition,
	'order' => 'ORDER BY pay_time DESC, id DESC',
	'size' => $pagesize,
	'offset' => $offset,
));

$pay_ids = Utility::GetColumn($orders, 'pay_id');
$pays = Table::Fetch('pay', $pay_ids);

$user_ids = Utility::GetColumn($orders, 'user_id');
$users = Table::Fetch('user', $user_ids);

$team_ids = Utility::GetColumn($orders, 'team_id');
$teams = Table::Fetch('team', $team_ids);

include template('biz_order_list');
